﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using BLL;
using Newtonsoft.Json;
using Model;
using System.Web.Security;
using System.Runtime.InteropServices;


namespace PropertyAppraisal
{
    public partial class Index1 : System.Web.UI.Page
    {
        private static AuthorityAndRolesBll arBll = new AuthorityAndRolesBll();


        protected void Page_Load(object sender, EventArgs e)
        {
            SetDateTimeFormat();
            //获取登录人角色
            rid = Convert.ToInt32(Session["id"]);
            Label1.Text = Session["UserName"].ToString();
        }

        [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]
        public static extern int GetSystemDefaultLCID();
        [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]
        public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);
        public const int LOCALE_SLONGDATE = 0x20;
        public const int LOCALE_SSHORTDATE = 0x1F;
        public const int LOCALE_STIME = 0x1003;
        public static void SetDateTimeFormat()
        {
            try
            {
                int x = GetSystemDefaultLCID();
                SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd");   //短日期格式    
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }  

        private static int rid;

        /// <summary>
        /// 退出清除Session登录到登录页面
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string BackLogin()
        {
            HttpContext.Current.Session["UserName"] = null;
            HttpContext.Current.Session.Remove("UserName");
            //从浏览器删除 Forms 身份验证票证。
            FormsAuthentication.SignOut();
            return "true";
        }

        /// <summary>
        /// 父级节点
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string Partent()
        {
            int Roid = rid;
            var Partent = arBll.Show().Where(m => m.parentId == 0 && m.roles_Id == Roid).ToList();
            //foreach (var tree in Partent)
            //{
            //    PartentId(tree.authority_Id);//调用子节点
            //}           
            return JsonConvert.SerializeObject(Partent);
        }

        /// <summary>
        /// 子级节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public static string PartentId(int id)
        {
            int Roid = rid;
            var zhi = arBll.Show().Where(m => m.parentId == id && m.roles_Id == Roid).ToList();
            return JsonConvert.SerializeObject(zhi);
        }
    }
}